import { defineStore } from "pinia";
import { LiveDataStore } from "./LiveDataStore";
import { RedPacketStore } from "./RedPacketStore";
import { watch } from "vue";
import { reward } from "../LiveNewApi";
import { debounce } from "lodash-es";
import { OnlineTimerStore } from "./OnlineTimer";
export const useRewardStore = defineStore("reward", () => {
  // 创建一个奖励队列
  const rewardQueue: number[] = [];
  let isProcessing = false;
  const REWARD_INTERVAL = 3000; // 奖励调用间隔，1秒

  const initWatcher = (
    liveDataStore: LiveDataStore,
    redPacketStore: RedPacketStore,
    onlineTimerStore: OnlineTimerStore
  ) => {
    if (liveDataStore.roomData.redPacket.openRedPacket == 0) {
      return;
    }
    // 添加奖励到队列
    const addToRewardQueue = (rewardType: number) => {
      if (!rewardQueue.includes(rewardType)) {
        rewardQueue.push(rewardType);
        processRewardQueue();
      }
    };

    // 处理奖励队列的函数
    const processRewardQueue = async () => {
      if (isProcessing || rewardQueue.length === 0) return;

      isProcessing = true;
      const rewardType = rewardQueue.shift();

      try {
        await reward({
          ...liveDataStore.roomParams,
          rewardType
        });
        // 更新对应的奖励状态
        switch (rewardType) {
          case 1:
            liveDataStore.roomWatchData.answerRewardState = 2; // 2表示已领取
            break;
          case 2:
            liveDataStore.roomWatchData.completedRewardState = 2;
            break;
          case 3:
            liveDataStore.roomWatchData.combineRewardState = 2;
            break;
        }
        onlineTimerStore.resetOnlineUpdater(liveDataStore);
      } finally {
        isProcessing = false;
        // 延迟处理下一个奖励
        setTimeout(() => {
          processRewardQueue();
        }, REWARD_INTERVAL);
      }
    };
    if (
      liveDataStore.roomData.redPacket.openRedPacket == 1 ||
      liveDataStore.roomData.redPacket.openRedPacket == 3
    ) {
      watch(
        [
          () => liveDataStore.roomWatchData.answerState,
          () => liveDataStore.roomWatchData.isSigned
        ],
        async ([answerState, isSigned]) => {
          if (answerState !== 1) return;
          if (liveDataStore.roomData.sign.signInRedPacket == 1 && isSigned == 0)
            return;
          if (liveDataStore.roomWatchData.answerRewardState !== 0) return;
          console.log("答题红包", answerState, isSigned);
          addToRewardQueue(1);
        }
      );
    }

    if (
      liveDataStore.roomData.redPacket.openRedPacket == 2 ||
      liveDataStore.roomData.redPacket.openRedPacket == 3
    ) {
      watch(
        [
          () => redPacketStore.isComplete,
          () => liveDataStore.roomWatchData.isSigned
        ],
        async ([isComplete, isSigned]) => {
          if (!isComplete) return;
          if (liveDataStore.roomData.sign.signInRedPacket == 1 && isSigned == 0)
            return;
          if (liveDataStore.roomWatchData.completedRewardState !== 0) return;
          console.log("时长红包", isComplete, isSigned);
          addToRewardQueue(2);
        }
      );
    }

    if (liveDataStore.roomData.redPacket.openRedPacket == 4) {
      watch(
        [
          () => redPacketStore.isComplete,
          () => liveDataStore.roomWatchData.answerState,
          () => liveDataStore.roomWatchData.isSigned
        ],
        async ([isComplete, answerState, isSigned]) => {
          if (!isComplete) return;
          if (answerState !== 1) return;
          if (liveDataStore.roomData.sign.signInRedPacket == 1 && isSigned == 0)
            return;
          if (liveDataStore.roomWatchData.combineRewardState !== 0) return;
          console.log("组合红包", isComplete, answerState, isSigned);
          addToRewardQueue(3);
        }
      );
    }
  };

  return {
    initWatcher
  };
});

export type RewardStore = ReturnType<typeof useRewardStore>;
